home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / ste / autoexec.lzh / SOURCE / AUTOEXEC.C next >
C/C++ Source or Header  |  1991-08-05  |  5KB  |  231 lines

  1. #include <tos.h>
  2.  
  3. #include "mxalloc.h"
  4. #include "player.h" 
  5. #include "header.h"
  6.  
  7.  
  8. #define AUTO_NAME "autoexec.sys"
  9.  
  10.  
  11. /* default free memory, below the sound sample */
  12. static long keep_free = 100000l; 
  13.  
  14. /* flag that tells if a sound sample is loaded */
  15. static int sound_loaded = 0;
  16.  
  17. /* copy of bit 1 (DMA) in the sound cookie */
  18. static int DMAsound = 0;
  19.  
  20. /* empty command line, for use when starting programs */
  21. const COMMAND empty_command = {0,""};
  22.  
  23. /* the most resent pressed key */
  24. static char CurrentKey = 0;
  25.  
  26. /* default key flag (don't wait on keypress) */
  27. static int DefaultKey = 0;
  28.  
  29.  
  30. long atol(char *s)
  31. /* me own StringToLong routine, using TC's costed 1 Kbyte !!! */
  32. {int n;
  33.  long l;
  34.  
  35.     l = 0;
  36.     while (*s)
  37.     {    n = *s++ - '0';
  38.         if (n>=0 && n<=9)
  39.             l = 10*l + n;
  40.         else
  41.             break;
  42.     }
  43.     return l;
  44.  
  45.  
  46. int execute_line(char *line)
  47. {int file, segm_repeat, no_exec;
  48.  long length;
  49.  char *tmp;
  50.     
  51.     no_exec = 0;
  52.     if (*line == '?') /* is it a conditional instruction ? */
  53.     {    no_exec = 1; /* if condition, then assume don't execute */
  54.         do
  55.         {    if (*++line == CurrentKey) /* condition true ? */
  56.                 no_exec = 0; /* ok, then execute rest of line... */
  57.             ++line;
  58.         } while (*line == '?'); /* is it still a conditional ? */
  59.     }
  60.     
  61.     if (no_exec) 
  62.         return 0;
  63.         
  64.     switch (*line++)
  65.     {    case 'X': /* break instruction execution */
  66.             return 1;
  67.             
  68.         case 'W': /* print a string and Wait for keyboard input */
  69.             Cconws(line);
  70.             if (!DefaultKey || Cconis())
  71.                 /* If there is a Key waiting in the keyboard buffer
  72.                  * OR there is no default key - then read key from buffer 
  73.                  */
  74.                 CurrentKey = (char)(Cconin() & 0xff);
  75.             Cconws("\r\n");
  76.             DefaultKey = 0;            
  77.             break;
  78.  
  79.         case 'P': /* print a string */
  80.             Cconws(line);
  81.             Cconws("\r\n");
  82.             break;
  83.             
  84.         case 'D': /* set default key for the next 'wait for key' */
  85.             DefaultKey = 1;
  86.             CurrentKey = *line;
  87.             break;
  88.             
  89.         case '>': /* start sound sample */
  90.             if (!DMAsound)
  91.                 break;
  92.             if (sound_loaded)
  93.             {    STe_end(0);
  94.                 sound_loaded = 0;
  95.             }
  96.             
  97.             if ( (file = Fopen(line, 0)) <= 0 )
  98.                 goto sn_err;
  99.  
  100.             length = Fseek(0, file, 2);
  101.             Fseek(0, file, 0);
  102.             
  103.             /* allocate memory for the 'free' section and the sound.
  104.              *    this way we don't put a sample where somone else expects
  105.              *    code or data...
  106.              */    
  107.             if ((tmp = Mxalloc(length+keep_free, MX_STON)) > 0l)
  108.             {    if (!LoadSegm(file, &segm_repeat, length, 
  109.                     (long *)tmp + keep_free))
  110.                 {    SetPlayFreq(PlayFreq);
  111.                     STe_segment((long *)tmp + keep_free, segm_repeat);
  112.                     sound_loaded = 1;
  113.                 }
  114.                 /* now free the memory, hoping that no one else allocate
  115.                  * it!!
  116.                  */            
  117.                 Mfree(tmp);
  118.             }
  119.             Fclose(file);
  120.             
  121.             if (!sound_loaded)
  122.                 goto sn_err;
  123.             break;
  124.  
  125.         case '#': /* set free memory, below samples */
  126.             keep_free = atol(line); 
  127.             break;
  128.             
  129.         case '!': /* execute program */
  130.             if (Pexec(0, line, &empty_command, 0l) < 0)
  131.                 goto ex_err;
  132.             break;
  133.             
  134.         default:;
  135.     }
  136.     return 0;
  137.     
  138. sn_err:
  139.     Cconws("AUTO: can't play the file: ");
  140.     goto w_name;
  141. ex_err:
  142.     Cconws("AUTO: Can't execute the file: ");
  143. w_name:
  144.     Cconws(line);
  145.     Cconws("\r\n");
  146.     
  147. NoLoad:
  148.     return 0;
  149. }
  150.  
  151.  
  152. main()
  153. {int file, store_line, skip_remaining;
  154.  long length, lines, l, snd;
  155.  char *p, *file_buffer;
  156.  char **line_buffer;
  157.     
  158.     /* turn on the cursor */
  159.     Cursconf(1, 0);
  160.     
  161.     /* check if the computer have DMA sound */
  162.     if (get_cookie('_SND', &snd) && (snd & 2)) /* DMA sound? */
  163.         DMAsound = 1;
  164.  
  165.     /* read the AUTO file into memory */
  166.     if ((file = Fopen(AUTO_NAME,0)) < 0)
  167.         goto op_err;
  168.     length = Fseek(0l,file,2);
  169.     Fseek(0l,file,0);
  170.     file_buffer = Mxalloc(length,MX_TTPR);
  171.     Fread(file,length,file_buffer);
  172.     Fclose(file);
  173.     
  174.     /* calculate an estimate of the number of lines */
  175.     p = file_buffer;
  176.     lines = 0;
  177.     for(l=0; l<length; l++)
  178.         if (*p++ == '\n')
  179.             lines += 4;
  180.     
  181.     line_buffer = Mxalloc(lines,MX_TTPR);
  182.     
  183.     /* now put pointers to each line into the lines array */
  184.     p = file_buffer;
  185.     store_line = 1;
  186.     skip_remaining = 0;
  187.     lines = 0;
  188.     for(l=0; l<length; l++)
  189.     {    if (*p == ';') /* comment */
  190.         {    skip_remaining = 1;
  191.             *p = '\0';
  192.         }
  193.  
  194.         /* is it a command line ? */
  195.         if (*p > ' ' && store_line && !skip_remaining)
  196.         {    line_buffer[lines++] = p;
  197.             store_line = 0;
  198.         }
  199.  
  200.         if (*p == '\n' || *p == '\r') /* end of line */
  201.         {    *p = '\0';
  202.             store_line = 1;
  203.             skip_remaining = 0;
  204.         }
  205.         ++p;
  206.     }
  207.     
  208.     /* execute each line in the file. */
  209.     l = 0;
  210.     while (lines--)
  211.         if (execute_line(line_buffer[l++]))
  212.             /* stop executing if execute_line() returns 1 */
  213.             break;
  214.     
  215.     if (sound_loaded)
  216.         STe_end(1);
  217.  
  218.     /* clean up, and return */
  219.     Mfree(line_buffer);
  220.     Mfree(file_buffer);
  221.  
  222.     return 0;
  223.  
  224. op_err:
  225.     Cconws(
  226.         "AUTO: can't open \"" 
  227.         AUTO_NAME
  228.         "\" for input!\r\n"); 
  229.     return 0;
  230. }